package ven.spots;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;


@SuppressWarnings("unused")
public class NodeListActivity extends Activity {
	
	private ListView nodeListView;
	private ArrayList<SpotNode> nodeList;
	private ArrayAdapter listAdapter;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.node_list_activity);
        
        nodeListView = (ListView)this.findViewById(R.id.nodeListView);
        nodeListView.setOnItemClickListener(this.getClickListener());
        
        /* TODO: Make instance of the database class.  Dump list of available
         * nodes into the nodeList ListView. */
        //nodeList = this.getTestNodes();
        DatabaseConnector2 dbc = new DatabaseConnector2();
        try {
			nodeList = dbc.getNodes("");
		} catch (IOException e) {
			Log.d("test", e.toString());
		}
        
        listAdapter = new NodeListViewAdapter(this, R.layout.node_list_row, nodeList);
        nodeListView.setAdapter(listAdapter);
    }
    
    
    /**
     * Class for filling the nodeListView object in this activity with
     * views that represent information gathered about SpotNodes.
     * @author Matthew Zizzi
     * 
     * Referenced: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
     */
    @SuppressWarnings("unchecked")
	private class NodeListViewAdapter extends ArrayAdapter {
    	private ArrayList<SpotNode> nodes;
    	
		public NodeListViewAdapter(Context context,
								   int textViewResourceId,
								   ArrayList<SpotNode> _nodes) {
			super(context, textViewResourceId, _nodes);
            this.nodes = _nodes;
		}
		
		@SuppressWarnings("static-access")
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi;
				vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.node_list_row, null);
			}
			
			SpotNode node = this.nodes.get(position);
			if (node != null) {
				TextView addressTV = (TextView)v.findViewById(R.id.ieeeTV);
				TextView statusTV  = (TextView)v.findViewById(R.id.statusTV);
				//Set text of the ieee textview
				if (addressTV != null) {
					addressTV.setText(node.getIeee());
				}
				//Set text based on the state of the SpotNode 'node'
				if (statusTV  != null) {
					switch (node.getState()) {
						case -1:
							statusTV.setText(R.string.unknown);
							statusTV.setTextColor(new Color().rgb(0xFF, 0x00, 0x00));
							break;
						case 0:
							statusTV.setText(R.string.offline);
							statusTV.setTextColor(new Color().rgb(0xFF, 0x00, 0x00));
							break;
						case 1:
							statusTV.setText(R.string.idle);
							statusTV.setTextColor(new Color().rgb(0x00, 0xCC, 0x00));
							break;
						case 2:
							statusTV.setText(R.string.flashing);
							statusTV.setTextColor(new Color().rgb(0xFF, 0xCC, 0x00));
							break;
					}//End switch
				}//End statusTV null check
			}//End node null check
			return v;
		}
    }//End NodeListViewAdapter
    
    /**
     * Create an onclick listener to be used by the nodeListView
     * object in this class.
     * @return The onclick listener to be used by the nodeListView
     * object in this class. 
     */
    private OnItemClickListener getClickListener() {
    	return (new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> av, View v,
					                int position, long id) {
				Intent intent = new Intent(NodeListActivity.this,
										   SpotManagerActivity.class);
				SpotNode node = nodeList.get(position);
				intent.putExtra("ieee", node.getIeee());
				intent.putExtra("state", node.getState());
				intent.putExtra("red", node.getRed());
				intent.putExtra("green", node.getGreen());
				intent.putExtra("blue", node.getBlue());
				startActivity(intent);
			}
    	});
    }
    
    /**
     * Tester method...  Used to grab an ArrayList<SpotNode> to
     * fill the listview in this activity
     * @return
     */
    private ArrayList<SpotNode> getTestNodes() {
    	SpotNode n1 = new SpotNode("0000.0000.0000.0000", -1);
    	SpotNode n2 = new SpotNode("0000.0000.0000.0000", 0);
    	SpotNode n3 = new SpotNode("0000.0000.0000.0000", 1);
    	SpotNode n4 = new SpotNode("1234.abcd.1234.abcd", 2);
    	ArrayList<SpotNode> a = new ArrayList<SpotNode>();
    	a.add(n1); a.add(n2); a.add(n3); a.add(n4);
    	return a;
    }
}